package com.ghost.view.pop.pack
{
	import com.ghost.UI.Ghost_Btn;
	import com.ghost.data_struct.HashMap;
	import com.ghost.interfaces.Ipop_win;
	import com.ghost.manager.Load_manager;
	import com.ghost.manager.Pack_manager;
	import com.ghost.manager.Pop_win_manager;
	import com.ghost.manager.Source_manager;
	import com.ghost.model.Configure_url;
	import com.ghost.model.Global_data;
	import com.ghost.model.Loading_data;
	import com.ghost.model.Object_pool;
	import com.ghost.view.login.Global_pop_bg;
	import com.ghost.view.pop.Pop_base;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextFormat;

	public class Pack extends Pop_base implements Ipop_win
	{
		private var bg_mc:Global_pop_bg;//背景
		private var pack_title_bd:BitmapData;
		private var lock_out_bd:BitmapData;
		private var lock_over_bd:BitmapData;
		private var cell_bg_bd:BitmapData;
		
		public var w:Number = 452;//pop的整体宽度
		public var h:Number;//pop的整体高度
		private var sider_num:Number = 22;//链条的个数
		private var frame_bg_num:Number = 9;//frame和bg_mc的左右间距
		private var frame_bg_vertical_num:Number = 30;//frame和bg_mc的下间距
		private var frame_bg_scaleGridLeft:Number = 7;//九宫格左边距
		private var frame_bg_scaleGridTop:Number = 7;//九宫格上边距
		
		private var end_bool:Boolean;//ui是否创建完成 
		private var pack_content_v:Vector.<Pack_content>;//5个Pack_content数组
		private var current_tab_num:int = 0;//当前tab序号
		public function Pack()
		{
			Load_manager.get_Instance().add(Configure_url.swf_url+"pack.swf",Loading_data.type_swf,Global_data.loading_des_list["pack.swf"].des_str,pack_Handler);
			Load_manager.get_Instance().start();
		}
		private function pack_Handler(data:Loader):void
		{
			pack_title_bd = Source_manager.getInstance().getBitmapData('pack_title');
			lock_out_bd = Source_manager.getInstance().getBitmapData('lock_out');
			lock_over_bd = Source_manager.getInstance().getBitmapData('lock_over');
			var sB:BitmapData = Source_manager.getInstance().getBitmapData('cell_bg');
			cell_bg_bd = new BitmapData(sB.width,sB.height);
			cell_bg_bd.copyChannel(sB,new Rectangle(0,0,sB.width,sB.height),new Point(0,0),BitmapDataChannel.GREEN,BitmapDataChannel.RED);
			cell_bg_bd.copyChannel(sB,new Rectangle(0,0,sB.width,sB.height),new Point(0,0),BitmapDataChannel.BLUE,BitmapDataChannel.BLUE);
			cell_bg_bd.copyChannel(sB,new Rectangle(0,0,sB.width,sB.height),new Point(0,0),BitmapDataChannel.RED,BitmapDataChannel.GREEN);
			var ui_data_h:HashMap = new HashMap();
			ui_data_h.put('pack_title',pack_title_bd);
			ui_data_h.put('lock_out',lock_out_bd);
			ui_data_h.put('lock_over',lock_over_bd);
			ui_data_h.put('cell_bg',cell_bg_bd);
			Object_pool.loaded_obj_pool.put("pack_ui",ui_data_h);
			Object_pool.icon_pool.put(0,new <Object>[new Bitmap(lock_out_bd),new Bitmap(lock_over_bd),new Bitmap(lock_over_bd),new Bitmap(lock_out_bd)])
			Pack_manager.getInstance().pack_main = this;
			this.type = 0;
			this.name = "pack";
			install_ui();
			this.back_mc = bg_mc;
			end_bool = true;
			h = this.height;
			ready_data();
			first_init();
			
			creat_win();
			init();
		}
		//准备数据
		private function ready_data():void
		{
			//Pack_manager.getInstance().pack_data_v = new <Vector.<Array>>[new <Array>[new Array(3,'2',0,10,1),new Array(6,'4',0,10,2),new Array(10,'5',0,10,0)],new <Array>[new Array(0,'1',0,10,0),new Array(3,'2',0,10,1),new Array(10,'5',0,10,0)],new <Array>[new Array(0,'1',0,10,0),new Array(3,'2',0,10,1)]]
			//Pack_manager.getInstance().parse_data();
		}
		//初始化ui
		private function install_ui():void
		{
			pack_content_v = new Vector.<Pack_content>();
			var title_img:Bitmap = new Bitmap(pack_title_bd);
			bg_mc = new Global_pop_bg(w,null,title_img,sider_num);
			this.addChild(bg_mc);
			var frame_bg_c:* = Global_data.grid_ui_h.get("frame_bg");
			var frame_bg:DisplayObject = new frame_bg_c();
			this.addChild(frame_bg);
			frame_bg.width = bg_mc.bg_mc.width-frame_bg_num*2;
			frame_bg.height = bg_mc.bg_mc.height-title_img.height-bg_mc.title_top_num-frame_bg_vertical_num*2;
			frame_bg.x = bg_mc.bg_mc.x+frame_bg_num;
			frame_bg.y = title_img.y+title_img.height+frame_bg_vertical_num;
			
			var tab_str_v:Vector.<String> = new <String>["一","二","三","四","五"];
			var textformate:TextFormat = new TextFormat(null,14,0xffffff);
			var tab_btn:Ghost_Btn;
			var i:int=0;
			for(;i<5;i++)
			{
				tab_btn = new Ghost_Btn(Global_data.grid_ui_h.get("tab_btn_v"),tab_Handler);
				
				tab_btn.setsize(70,27);
				tab_btn.name = String(i);
				tab_btn.label="背包"+tab_str_v[i];
				tab_btn.textformat = textformate;
				tab_btn.x = frame_bg.x+frame_bg_scaleGridLeft+i*(tab_btn.width+int((frame_bg.width-frame_bg_scaleGridLeft*2-5*tab_btn.width)/5));
				tab_btn.y = frame_bg.y-tab_btn.height;
				this.addChild(tab_btn);
				if(i==0)
				{
					tab_btn.enabled=false;
					current_selected_btn=tab_btn;
				}
			}
			var j:int = 0;
			for(;j<5;j++)
			{
				var pack_content:Pack_content = new Pack_content();
				pack_content.max_w = frame_bg.width-frame_bg_scaleGridLeft*2;
				pack_content.max_h = frame_bg.height-frame_bg_scaleGridTop*2;
				this.addChild(pack_content);
				pack_content.visible = false;
				pack_content_v.push(pack_content);
				pack_content.init(j);
				pack_content.x = frame_bg.x+frame_bg_scaleGridLeft+(pack_content.max_w-pack_content_v[0].width)/2;
				pack_content.y = frame_bg.y+frame_bg_scaleGridTop+(pack_content.max_h-pack_content_v[0].height)/2;
			}
			Pack_manager.getInstance().content_v = pack_content_v;
		}
		private var current_selected_btn:Ghost_Btn;
		private function tab_Handler(e:MouseEvent):void
		{
			current_selected_btn.enabled = true;
			current_selected_btn = Ghost_Btn(e.target);
			current_selected_btn.enabled = false;
			pack_content_v[current_tab_num].visible = false;
			current_tab_num = int(current_selected_btn.name);
			pack_content_v[current_tab_num].visible = true;
			Pack_manager.getInstance().current_tab = current_tab_num;
			if(pack_content_v[current_tab_num].tab_num==-1)pack_content_v[current_tab_num].show(current_tab_num);
		}
		//初始化数据
		private function first_init():void
		{
			pack_content_v[0].visible = true;
			pack_content_v[0].show(0);
		}
		override public function close_all():void
		{
			Pop_win_manager.getInstance().Remove_pop_win(this);
			//移除其他事件
		}
		public function creat_win():void
		{
			if(!Pop_win_manager.getInstance().pop_main.contains(this))
			{
				if(end_bool)
				{
					Pop_win_manager.getInstance().Creat_pop_win(this,true);
					//this.first_init();
				}
			}
		}
	}
}